package com.witmore.ftp.web.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * 应用启动监听器
 * 在应用启动完成后打印访问链接
 */
@Slf4j
@Component
public class ApplicationStartupListener {

    @Value("${server.port:8080}")
    private int serverPort;

    @Value("${ftp.server.port:21}")
    private int ftpPort;

    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {
        try {
            String hostAddress = InetAddress.getLocalHost().getHostAddress();
            String hostName = InetAddress.getLocalHost().getHostName();
            
            log.info("");
            log.info("=".repeat(80));
            log.info("🚀 FTP管理系统启动成功！");
            log.info("=".repeat(80));
            log.info("");
            log.info("📊 管理界面访问地址:");
            log.info("   ├─ 本地访问: http://localhost:{}/admin", serverPort);
            log.info("   ├─ 局域网访问: http://{}:{}/admin", hostAddress, serverPort);
            log.info("   └─ 主机名访问: http://{}:{}/admin", hostName, serverPort);
            log.info("");
            log.info("🌐 API接口地址:");
            log.info("   ├─ 系统状态: http://localhost:{}/api/ftp/status", serverPort);
            log.info("   ├─ 用户管理: http://localhost:{}/api/ftp/users", serverPort);
            log.info("   ├─ 操作日志: http://localhost:{}/api/ftp/file-logs", serverPort);
            log.info("   └─ 连接管理: http://localhost:{}/api/ftp/connections", serverPort);
            log.info("");
            log.info("📁 FTP服务器信息:");
            log.info("   ├─ FTP端口: {}", ftpPort);
            log.info("   ├─ 连接地址: ftp://localhost:{}", ftpPort);
            log.info("   └─ 测试命令: lftp -u username,password localhost -p {}", ftpPort);
            log.info("");
            log.info("🎯 功能模块:");
            log.info("   ├─ 📊 仪表盘: /admin");
            log.info("   ├─ 👥 用户管理: /admin/users");
            log.info("   ├─ 📋 操作日志: /admin/logs");
            log.info("   ├─ 🔌 连接管理: /admin/connections");
            log.info("   └─ 📈 系统监控: /admin/monitor");
            log.info("");
            log.info("=".repeat(80));
            log.info("💡 提示: 在浏览器中打开管理界面开始使用！");
            log.info("=".repeat(80));
            log.info("");
            
        } catch (Exception e) {
            log.error("获取主机信息失败", e);
            log.info("");
            log.info("=".repeat(60));
            log.info("🚀 FTP管理系统启动成功！");
            log.info("📊 管理界面: http://localhost:{}/admin", serverPort);
            log.info("📁 FTP端口: {}", ftpPort);
            log.info("=".repeat(60));
            log.info("");
        }
    }
}
